package com.intelligent.ispc.core.service.impl;

import com.intelligent.ispc.cache.SpcDataCacheFactory;
import com.intelligent.ispc.common.mapper.JsonMapper;
import com.intelligent.ispc.common.utils.ApplicationException;
import com.intelligent.ispc.common.utils.ExceptionMessages;
import com.intelligent.ispc.common.utils.ResourceBundleUtils;
import com.intelligent.ispc.core.service.DataImportService;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Eligi.Ran on 2016/9/28.
 */
public class DataImportServiceImpl implements DataImportService {

    @AutoConfigureOrder
    private SpcDataCacheFactory spcDataCacheFactory;

    private JsonMapper jsonMapper = JsonMapper.nonDefaultMapper();

    @Override
    public Map<String, Object> getAllData(List<String> filePath) {
        try {
            String spcData = spcDataCacheFactory.getCacheSpcData().get(jsonMapper.toJson(filePath));
            return jsonMapper.fromJson(spcData, jsonMapper.contructMapType(Map.class, String.class, Object.class));
        } catch (Exception e) {
            throw new ApplicationException(ExceptionMessages.ERR_15003, ResourceBundleUtils.getString(ExceptionMessages.EXCEPTION_GLOBAL_PYTHON_SCRIPT_ERROR));
        }
    }

    @Override
    public Map<String, Object> getFieldData(List<String> filePath) {
        try {
            String spcData = spcDataCacheFactory.getCacheSpcFieldData().get(jsonMapper.toJson(filePath));
            return jsonMapper.fromJson(spcData, jsonMapper.contructMapType(Map.class, String.class, Object.class));
        } catch (Exception e) {
            throw new ApplicationException(ExceptionMessages.ERR_15003, ResourceBundleUtils.getString(ExceptionMessages.EXCEPTION_GLOBAL_PYTHON_SCRIPT_ERROR));
        }
    }

    @Override
    public Map<String, Object> getSpecificData(List<String> filePath, List<String> itemNames) {
        Map<String, Object> inputParams = new HashMap<>();
        inputParams.put("filePaths", filePath);
        inputParams.put("itemNames", itemNames);
        try {
            String spcData = spcDataCacheFactory.getCacheSpcFieldData().get(jsonMapper.toJson(inputParams));
            return jsonMapper.fromJson(spcData, jsonMapper.contructMapType(Map.class, String.class, Object.class));
        } catch (Exception e) {
            throw new ApplicationException(ExceptionMessages.ERR_15003, ResourceBundleUtils.getString(ExceptionMessages.EXCEPTION_GLOBAL_PYTHON_SCRIPT_ERROR));
        }
    }


    @Override
    public String getCustomizedPScript() {
        return null;
    }
}

